<?php
class comment_model extends Zmodel{
	
	/* --- VOTE COMMENT --- */
	
		// SELECT COMMENT
	function SelectVoteComment($event_id=null){
		$this->load->database();
		$conditional_string=($event_id==null)?"":" WHERE V.event_id="$event_id;
		$sql="SELECT C.comment_id,V.event_id,C.description,C.date_post,C.user_post,C.date_update FROM ";
		$sql+="user_comment AS C INNER JOIN vote_comment AS V ON C.comment_id=V.comment_id".$conditional_string;
		$sql+=" ORDER BY C.date_post DESC";
		return $this->db->query($sql);
	}
		// INSERT COMMENT
	function InsertVoteComment($event_id,$comment_id,$description){
		$this->load->database();
		$Z_USER = Zsession::getUserFromSession();
	    $user_post=$Z_USER["user_id"];
		$sql1="INSERT INTO user_comment(description,date_post,user_post) VALUES('".pg_escape_string($description)."',now(),'".pg_escape_string($user_post)."')";
		$sql2="INSERT INTO vote_comment(event_id,comment_id) VALUES(".$event_id.",".$comment_id.")";
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
		// DELETE COMMENT
	function DeleteVoteComment($comment_id){
		$this->load->database();
		$sql1="DELETE FROM vote_comment WHERE comment_id=".$comment_id;
		$sql2="DELETE FROM user_comment WHERE comment_id=".$comment_id;
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
	
		// UPDATE COMMENT
	function UpdateVoteComment($comment_id,$description){
		$this->load->database();
		$sql="UPDATE user_comment SET description='".pg_escape_string($description)."',date_update=now() WHERE comment_id="+$comment_id;
		$this->db->query($sql);
	}

	
	/* --- JOB WANTED COMMENT --- */
	
		// SELECT COMMENT
	function SelectJobWantedComment($job_wan_id=null){
		$this->load->database();
		$conditional_string=($job_wan_id==null)?"":" WHERE J.job_wan_id="$job_wan_id;
		$sql="SELECT C.comment_id,J.job_wan_id,C.description,C.date_post,C.user_post,C.date_update FROM ";
		$sql+="user_comment AS C INNER JOIN job_wanted_comment AS J ON C.comment_id=J.comment_id".$conditional_string;
		$sql+=" ORDER BY C.date_post DESC";
		return $this->db->query($sql);
	}
		// INSERT COMMENT
	function InsertJobWantedComment($job_wan_id,$comment_id,$description){
		$this->load->database();
		$Z_USER = Zsession::getUserFromSession();
	    $user_post=$Z_USER["user_id"];
		$sql1="INSERT INTO user_comment(description,date_post,user_post) VALUES('".pg_escape_string($description)."',now(),'".pg_escape_string($user_post)."')";
		$sql2="INSERT INTO job_wanted_comment(job_wan_id,comment_id) VALUES(".$job_wan_id.",".$comment_id.")";
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
		// DELETE COMMENT
	function DeleteJobWantedComment($comment_id){
		$this->load->database();
		$sql1="DELETE FROM job_wanted_comment WHERE comment_id=".$comment_id;
		$sql2="DELETE FROM user_comment WHERE comment_id=".$comment_id;
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
	
		// UPDATE COMMENT
	function UpdateJobWantedComment($comment_id,$description){
		$this->load->database();
		$sql="UPDATE user_comment SET description='".pg_escape_string($description)."',date_update=now() WHERE comment_id="+$comment_id;
		$this->db->query($sql);
	}
	
	
	/* --- JOB ANNOUNCEMENT COMMENT --- */
	
		// SELECT COMMENT
	function SelectJobAnnouncementComment($announ_id=null){
		$this->load->database();
		$conditional_string=($announ_id==null)?"":" WHERE J.announ_id="$announ_id;
		$sql="SELECT C.comment_id,J.announ_id,C.description,C.date_post,C.user_post,C.date_update FROM ";
		$sql+="user_comment AS C INNER JOIN job_announcement_comment AS J ON C.comment_id=J.comment_id".$conditional_string;
		$sql+=" ORDER BY C.date_post DESC";
		return $this->db->query($sql);
	}
		// INSERT COMMENT
	function InsertJobAnnouncementComment($announ_id,$comment_id,$description){
		$this->load->database();
		$Z_USER = Zsession::getUserFromSession();
	    $user_post=$Z_USER["user_id"];
		$sql1="INSERT INTO user_comment(description,date_post,user_post) VALUES('".pg_escape_string($description)."',now(),'".pg_escape_string($user_post)."')";
		$sql2="INSERT INTO job_announcement_comment(announ_id,comment_id) VALUES(".$announ_id.",".$comment_id.")";
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
		// DELETE COMMENT
	function DeleteJobAnnouncementComment($comment_id){
		$this->load->database();
		$sql1="DELETE FROM job_announcement_comment WHERE comment_id=".$comment_id;
		$sql2="DELETE FROM user_comment WHERE comment_id=".$comment_id;
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
	
		// UPDATE COMMENT
	function UpdateJobAnnouncementComment($comment_id,$description){
		$this->load->database();
		$sql="UPDATE user_comment SET description='".pg_escape_string($description)."',date_update=now() WHERE comment_id="+$comment_id;
		$this->db->query($sql);
	}
	
	
	/* --- CLASSIFIED ADS COMMENT --- */
	
		// SELECT COMMENT
	function SelectClassifiedAdsComment($product_id=null){
		$this->load->database();
		$conditional_string=($product_id==null)?"":" WHERE P.product_id="$product_id;
		$sql="SELECT C.comment_id,P.product_id,C.description,C.date_post,C.user_post,C.date_update FROM ";
		$sql+="user_comment AS C INNER JOIN classified_ads_comment AS P ON C.comment_id=P.comment_id".$conditional_string;
		$sql+=" ORDER BY C.date_post DESC";
		return $this->db->query($sql);
	}
		// INSERT COMMENT
	function InsertClassifiedAdsComment($product_id,$comment_id,$description){
		$this->load->database();
		$Z_USER = Zsession::getUserFromSession();
	    $user_post=$Z_USER["user_id"];
		$sql1="INSERT INTO user_comment(description,date_post,user_post) VALUES('".pg_escape_string($description)."',now(),'".pg_escape_string($user_post)."')";
		$sql2="INSERT INTO classified_ads_comment(product_id,comment_id) VALUES(".$product_id.",".$comment_id.")";
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
		// DELETE COMMENT
	function DeleteClassifiedAdsComment($comment_id){
		$this->load->database();
		$sql1="DELETE FROM classified_ads_comment WHERE comment_id=".$comment_id;
		$sql2="DELETE FROM user_comment WHERE comment_id=".$comment_id;
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
	
		// UPDATE COMMENT
	function UpdateClassifiedAdsComment($comment_id,$description){
		$this->load->database();
		$sql="UPDATE user_comment SET description='".pg_escape_string($description)."',date_update=now() WHERE comment_id="+$comment_id;
		$this->db->query($sql);
	}
	
	
	/* --- PROPERTY COMMENT --- */
	
		// SELECT COMMENT
	function SelectPropertyComment($property_id=null){
		$this->load->database();
		$conditional_string=($property_id==null)?"":" WHERE P.property_id="$property_id;
		$sql="SELECT C.comment_id,P.property_id,C.description,C.date_post,C.user_post,C.date_update FROM ";
		$sql+="user_comment AS C INNER JOIN property_comment AS P ON C.comment_id=P.comment_id".$conditional_string;
		$sql+=" ORDER BY C.date_post DESC";
		return $this->db->query($sql);
	}
		// INSERT COMMENT
	function InsertPropertyComment($property_id,$comment_id,$description){
		$this->load->database();
		$Z_USER = Zsession::getUserFromSession();
	    $user_post=$Z_USER["user_id"];
		$sql1="INSERT INTO user_comment(description,date_post,user_post) VALUES('".pg_escape_string($description)."',now(),'".pg_escape_string($user_post)."')";
		$sql2="INSERT INTO property_comment(property_id,comment_id) VALUES(".$property_id.",".$comment_id.")";
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
		// DELETE COMMENT
	function DeletePropertyComment($comment_id){
		$this->load->database();
		$sql1="DELETE FROM property_comment WHERE comment_id=".$comment_id;
		$sql2="DELETE FROM user_comment WHERE comment_id=".$comment_id;
		$this->db->query($sql1);
		$this->db->query($sql2);
	}
	
		// UPDATE COMMENT
	function UpdatePropertyComment($comment_id,$description){
		$this->load->database();
		$sql="UPDATE user_comment SET description='".pg_escape_string($description)."',date_update=now() WHERE comment_id="+$comment_id;
		$this->db->query($sql);
	}
}

?>